#include "stdafx.h"
#include "detect.h"

HANDLE CDetect::m_hVectorHander;
CDetect::MapHook CDetect::m_mapMapHook;
CRITICAL_SECTION CDetect::m_CriticalSection;

CDetect::CDetect()
{
    InitializeCriticalSection(&m_CriticalSection);
    m_mapMapHook.clear();
    m_hVectorHander = AddVectoredExceptionHandler(1, CDetect::VectoredHandler);
}

CDetect::~CDetect()
{
    if (m_hVectorHander)
    {
        RemoveVectoredExceptionHandler(m_hVectorHander);
        m_hVectorHander = NULL;
    }
    m_mapMapHook.clear();
    DeleteCriticalSection(&m_CriticalSection);
}

LONG WINAPI CDetect::VectoredHandler(PEXCEPTION_POINTERS ExceptionInfo)
{
    LONG lRet = EXCEPTION_CONTINUE_SEARCH;
    EnterCriticalSection(&m_CriticalSection);

    MapHook::iterator itr = m_mapMapHook.begin();

    for (itr = m_mapMapHook.begin(); itr != m_mapMapHook.end(); ++itr)
    {
        if (itr->first == (HANDLE)ExceptionInfo->ContextRecord->Eip)
        {
            lRet = itr->second.fun->run(itr->first, ExceptionInfo);
            break;
        }
    }


    LeaveCriticalSection(&m_CriticalSection);
    return lRet;
}


class CReadProcessMemory:public IFunctioner
{
public:
    virtual LONG run(HANDLE hFun, PEXCEPTION_POINTERS pEI)
    {
        pEI->ContextRecord->Eip = (DWORD)CReadProcessMemory::MyReadProcesssMemory;
    }
    static BOOL MyReadProcesssMemory(HANDLE h, LPCVOID pAddress,
    LPVOID pBuf, SIZE_T nSize, SIZE_T* pRead)
    {

    }
};